using UnityEngine;


namespace HyxFrame.Core
{
    /*
    * Introduction：    通用Mono单例基类，继承MonoBase，自动管理实例生命周期，支持DontDestroyOnLoad
    * Version：         1.0
    * Creator：         yixiang.hui
    * Creation Time：   2025.9.1
    *
    * Last Editor：     yixiang.hui 
    * Last Edit Time：  2025.9.1
    * Note：            1.0版本完成基本功能
    */

    public abstract class MonoSingleton<T> : MonoBase where T : MonoBase
    {
        private static T instance;
        private static bool applicationQuitting = false;
        private static readonly object lockObj = new object();

        /// <summary>
        /// 获取单例实例
        /// </summary>
        public static T Instance
        {
            get
            {
                if (applicationQuitting) return null;
                if (instance == null)
                {
                    lock (lockObj)
                    {
                        if (instance == null)
                        {
                            // 优先查找场景中已有实例
                            instance = CreateInstance();
                        }
                    }
                }
                return instance;
            }
        }

        /// <summary>
        /// 创建单例实例（可指定父物体）
        /// </summary>
        public static T CreateInstance(GameObject parent = null)
        {
            if (applicationQuitting) return null;
            if (instance != null) return instance;

            lock (lockObj)
            {
                if (instance == null)
                {
                    GameObject singletonObj = new GameObject($"_{typeof(T).Name}");
                    if (parent != null)
                        singletonObj.transform.SetParent(parent.transform, false);
                    instance = singletonObj.AddComponent<T>();
                }
            }
            return instance;
        }

        /// <summary>
        /// 创建全局单例（场景切换不销毁）
        /// </summary>
        public static T CreateDontDestroyInstance(GameObject parent = null)
        {
            if (applicationQuitting) return null;
            if (instance != null) return instance;

            lock (lockObj)
            {
                if (instance == null)
                {
                    GameObject singletonObj = new GameObject($"_{typeof(T).Name}");
                    if (parent != null)
                        singletonObj.transform.SetParent(parent.transform, false);
                    DontDestroyOnLoad(singletonObj);
                    instance = singletonObj.AddComponent<T>();
                }
            }
            return instance;
        }

        /// <summary>
        /// 单例初始化时调用，可重写
        /// </summary>
        protected virtual void OnCreateInstance() { InitManager(); }

        protected virtual void Awake()
        {
            if (instance == null)
            {
                instance = this as T;
                OnCreateInstance();
            }
            else if (instance != this)
            {
                DestroyImmediate(this.gameObject);
            }
        }

        protected virtual void OnDestroy()
        {
            if (instance == this)
            {
                instance = null;
                applicationQuitting = true;
            }
        }

        protected virtual void OnApplicationQuit()
        {
            applicationQuitting = true;
        }


        protected abstract void InitManager();

    }

}